package com.lx.service;

import com.lx.dao.AdminDao;
import com.lx.entity.Admin;
import com.lx.entity.User;
import javafx.scene.AmbientLight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminDao adminDao;

    /**
     * 管理员登陆
     * @param name
     * @param password
     * @return
     */
    public Admin adminLogin(String name, String password){
        return adminDao.adminLogin(name,password);
    }

    /**
     * 注册管理员
     * @param name
     * @param password
     * @return
     */
    public Admin adminRegister(String name,String password){
        Admin isAdmin = adminDao.getIsAdmin(name);
        if (isAdmin == null){
            adminDao.adminRegister(name, password);
            return adminDao.getIsAdmin(name);
        }else {
            return null;
        }
    }
    //修改密码
    public boolean updateAdmin(Admin admin,String oldPassword,String passwords){
        //根据用户名查询用户
        Admin isAdmin = adminDao.getIsAdmin(admin.getName());
        //输入的密码与数据库比对
        if (oldPassword.equals(isAdmin.getPassword())&&passwords.equals(admin.getPassword())){
            return adminDao.updateAdmin(admin);
        }else {
            return false;
        }
    }

    //查询所有用户
    public List<User> getUser(int page,int pageSize){
        return adminDao.getUser((page - 1) * pageSize, pageSize);
    }

    //查询所有用户数量
    public int countUser(){
        return adminDao.countUser().size();
    }
}
